/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import blueprint4j.Constants;

public class FieldGroupGPSLocation extends FieldGroup {
	private static final long serialVersionUID = 1L;
	public FieldStringRegEx latitudeString;
	public FieldStringRegEx longitudeString;
	public FieldAction change;

	public FieldGroupGPSLocation(String name,String prefix,Entity entity) {
		super(name,Constants.ICON_BASE+"apps/agt_internet.png",entity);
		if (prefix==null){
			prefix="";
		}
		if (prefix.length()>0){
			prefix=prefix+"_";
		}
		latitudeString = new FieldStringRegEx(prefix+"gpslatitude", 0, "Latitude",20,"^\\d{1,3}[:]\\d{1,2}[.]\\d{1,5}[NS]$","Format is 12:34.555E or W",entity);
		longitudeString = new FieldStringRegEx(prefix+"gpslongitude", 0, "Longitude",20,"^\\d{1,3}[:]\\d{1,2}[.]\\d{1,5}[EW]$","Format is 54:32.111N or S",entity);
	}
	
	//Decimal Degrees = Degrees + minutes/60 + seconds/3600
	public Double getLatitude(){
		String val=latitudeString.get();
		Double deg=null;
		if (val!=null && latitudeString.isInvalid()==null){
			deg=Double.valueOf(val.substring(0,val.indexOf(":")))+Double.valueOf(val.substring(val.indexOf(":")+1,val.indexOf(".")))/60+Double.valueOf(val.substring(val.indexOf(".")+1,val.length()-1))/3600;
			if (val.endsWith("S")){
				deg=-deg;
			}
		}
		return deg;
	}

	public void setLatitude(Double deg){
		String val=null;
		if (deg!=null){
			String dir=deg<0?"S":"N";
			double d=deg<0?-deg:deg;
			val=""+((int)d);
			d-=((int)d);
			d*=60;
			val+=":"+((int)d);
			d-=((int)d);
			d*=3600;
			val+="."+((int)d);
			val+=dir;
		}
		latitudeString.set(val);
	}

	//Decimal Degrees = Degrees + minutes/60 + seconds/3600
	public Double getLongitude(){
		String val=longitudeString.get();
		Double deg=null;
		if (val!=null && longitudeString.isInvalid()==null){
			deg=Double.valueOf(val.substring(0,val.indexOf(":")))+Double.valueOf(val.substring(val.indexOf(":")+1,val.indexOf(".")))/60+Double.valueOf(val.substring(val.indexOf(".")+1,val.length()-1))/3600;
			if (val.endsWith("W")){
				deg=-deg;
			}
		}
		return deg;
	}

	public String getLongitudeString(){
		return longitudeString.get();
	}
	

	public String getLatitudeString(){
		return latitudeString.get();
	}
	
	public void setLongitudeString(String val){
		longitudeString.set(val);
	}

	public void setLatitudeString(String val){
		latitudeString.set(val);
	}
	
	public void setLongitude(Double deg){
		String val=null;
		if (deg!=null){
			String dir=deg<0?"W":"E";
			double d=deg<0?-deg:deg;
			val=""+((int)d);
			d-=((int)d);
			d*=60;
			val+=":"+((int)d);
			d-=((int)d);
			d*=3600;
			val+="."+((int)d);
			val+=dir;
		}
		longitudeString.set(val);
	}
}

